package org.diffshop.product.controller;

import org.diffshop.common.utils.PageUtils;
import org.diffshop.common.utils.R;
import org.diffshop.product.entity.ProductAttrValueEntity;
import org.diffshop.product.service.PmsAttrService;
import org.diffshop.product.service.ProductAttrValueService;
import org.diffshop.product.vo.AttrVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 商品属性
 *
 * @author tjf
 * @date 2024-05-08 16:04:20
 */
@RestController
@RequestMapping("product/pmsattr")
public class PmsAttrController {
    @Autowired
    private PmsAttrService pmsAttrService;
    @Autowired
    private ProductAttrValueService productAttrValueService;
    /**
     * 修改，根据spu修改
     * @param spuId
     * @param attrValueEntities
     * @return
     */
    @PostMapping("/update/{spuId}")
    public R update(@PathVariable("spuId") Long spuId, @RequestBody List<ProductAttrValueEntity> attrValueEntities){
        productAttrValueService.update(spuId, attrValueEntities);

        return R.ok();
    }
    /**
     * 查询Spu的基本属性
     * @param spuId
     * @return
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R queryBaseAttrBySpuId(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> data = productAttrValueService.queryBaseAttrBySpuId(spuId);

        return R.ok().put("data", data);
    }

    /**
     * 查询某个分类关联的所有属性
     * @param type
     * @param catId
     * @return
     */
    @GetMapping("/{type}/list/{catId}")
    public R queryAttrsByCatId(@RequestParam Map<String, Object> params,
                               @PathVariable("type") String type,
                               @PathVariable("catId") Long catId){
        PageUtils page = pmsAttrService.getAttrsByCatId(params, type, catId);

        return R.ok().put("page", page);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = pmsAttrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
		AttrVO attrVO = pmsAttrService.queryDetail(attrId);
        return R.ok().put("attr", attrVO);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVO attrVO){
		pmsAttrService.saveAttr(attrVO);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVO attrVO){
		pmsAttrService.updateDetail(attrVO);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		pmsAttrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
